package com.alisls.shequtao.service.goods.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * Token配置类
 *
 * @author Ke Wang
 */
@Configuration
public class TokenConfig {

    /**
     * JwtToken对称加密密钥
     */
    @Value("${oauth.jwtToken.symmetric.signingKey}")
    private String signingKey;

    /**
     * 向容器注入JWT转换器JwtAccessTokenConverter
     * @return
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        // 采用对称加密签名令牌，对应资源服务器也要采用此密钥进行解密
        jwtAccessTokenConverter.setSigningKey(signingKey);

        /*
         * 采用非对称加密解密 JWT
         */
        /*ClassPathResource classPathResource = new ClassPathResource("publicKey.txt");
        String publicKey = null;
        try {
            publicKey = IOUtils.toString(classPathResource.getInputStream(), Charset.forName("UTF-8"));
        } catch (IOException e) {
            System.out.println("读取到公钥：" + publicKey);
        }

        jwtAccessTokenConverter.setVerifierKey(publicKey);*/
        return jwtAccessTokenConverter;
    }

    /**
     * 令牌管理策略
     * 向容器注入 jwtTokenStore
     * @return jwtTokenStore示例
     */
    @Bean
    public TokenStore jwtTokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

}
